part of '../repository.dart';

const _kCollectionidKey = 'id';
const _kCollectionTitlesKey = 'titles';
const _kCollectionLexicalItemIdsKey = 'lexical_item_ids';

const _kCollectionListItemCollectionType = 'inbuilt_collection';

class _CollectionDto extends Equatable implements _CollectionListItemDto {
  const _CollectionDto._internal({
    required this.id,
    required this.titles,
    required this.lexicalItemIds,
  });

  final String id;
  final BuiltMap<String, String> titles;
  final BuiltSet<String> lexicalItemIds;

  @override
  String get type => _kCollectionListItemCollectionType;

  @override
  List<Object?> get props {
    return [
      id,
      titles,
      lexicalItemIds,
    ];
  }

  factory _CollectionDto.fromJson(Map<String, dynamic> json) {
    final titleMap = json[_kCollectionTitlesKey] as Map;

    final titles = titleMap.map((languageCode, title) {
      return MapEntry(languageCode as String, title as String);
    }).build();

    final lexicalItemIds = BuiltSet<String>.from(json[_kCollectionLexicalItemIdsKey]);

    return _CollectionDto._internal(
      id: json[_kCollectionidKey],
      titles: titles,
      lexicalItemIds: lexicalItemIds,
    );
  }

  @override
  AssetCollection toEntity() {
    return AssetCollection(
      id: id,
      titles: titles,
      lexicalItemIds: lexicalItemIds,
    );
  }
}
